//
//  WPImageViewController.m
//  打马赛克Demo
//
//  Created by WGP on 16/8/8.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPImageViewController.h"
#import "DrawImageView.h"

#define KScreenWidth [UIScreen mainScreen].bounds.size.width
#define KScreenHeight [UIScreen mainScreen].bounds.size.height

@interface WPImageViewController ()

@property (nonatomic, weak) DrawImageView *imageView;
@property(nonatomic,strong) UIImagePickerController *picker;

@end

@implementation WPImageViewController

-(instancetype)initWithVC:(UIImagePickerController *)picker
{
    WPImageViewController *vc = [[WPImageViewController alloc] init];
    vc.picker = picker;
    return vc;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [UIApplication sharedApplication].statusBarHidden = NO;
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    DrawImageView * imageV = [[DrawImageView alloc]initWithFrame:[self caculatorImageFrame]];
    imageV.image = self.image;
    self.imageView = imageV;
    [self.view addSubview:imageV];
    [self setNavUI];
    
}

- (void)setNavUI
{
    UIView *navView = [UIView new];
//    navView.backgroundColor = [UIColor orangeColor];
    navView.frame = CGRectMake(0, 20, KScreenWidth, 44);
    [self.view addSubview:navView];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"保存" forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:16];
    [btn setTitleColor:RGB_SAME(51) forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(leftBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [btn sizeToFit];
    [navView addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(navView);
        make.right.mas_equalTo(-10);
    }];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"图片编辑";
    titleLabel.textColor = RGB_SAME(51);
    titleLabel.font = [UIFont systemFontOfSize:18];
    [navView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(navView);
    }];
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 22, 22);
    [leftButton setBackgroundImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
    
    [leftButton addTarget:self action:@selector(leftBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:leftButton];
    
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(navView);
        make.left.mas_equalTo(10);
    }];
}

- (void)rightBtnClick
{
    UIImage *image = [self.imageView saveImage];
    [self.delegate WPImageViewController:self Image:image];
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
- (void)leftBtnClick
{
//    [self.nav dismissViewControllerAnimated:YES completion:nil];
}

- (CGRect)caculatorImageFrame
{
    if (self.image) {
        CGFloat imageH = self.image.size.height * KScreenWidth/self.image.size.width;
        CGFloat x = 0;
        CGFloat y = (KScreenHeight - imageH) / 2;
        return CGRectMake(x, y, KScreenWidth, imageH);
    }
    return CGRectZero;
}

@end
